package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.Elefee;
import com.qf.entity.Owner;
import com.qf.entity.Park;
import com.qf.entity.Parking;
import com.qf.enums.AppHttpCodeEnum;
import com.qf.mapper.OwnerMapper;
import com.qf.mapper.ParkMapper;
import com.qf.mapper.ParkingMapper;
import com.qf.result.ResponseResult;
import com.qf.service.ParkingService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Parking)表服务实现类
 *
 * @author makejava
 * @since 2023-02-04 22:31:48
 */
@Service("parkingService")
public class ParkingServiceImpl extends ServiceImpl<ParkingMapper, Parking> implements ParkingService {

    @Resource
    ParkingMapper parkingMapper;

    @Resource
    ParkMapper parkMapper;

    @Resource
    OwnerMapper ownerMapper;

    @Resource
    OwnerMapper mapper;

    @Override
    public ResponseResult findAll(String ownerName) {


        List<Parking> parkings= new ArrayList<>();

        if (ownerName==null||"".equals(ownerName)) {
            parkings = parkingMapper.selectList(null);
            for (Parking parking :parkings){
                Integer ownId = Integer.parseInt(parking.getOwnId());
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("id", ownId);
                Owner owner = ownerMapper.selectOne(queryWrapper1);
                parking.setOwnerName(owner.getOwnName());
                parking.setOwnerPhone(owner.getOwnPhone());
            }
        }else {
            QueryWrapper<Owner> queryWrapper = new QueryWrapper();
            queryWrapper.like("own_name", ownerName) //业主姓名
                    .or()
                    .like("car_num",ownerName)  //业主车牌
                    .or()
                    .like("own_phone",ownerName); //手机号
//                    .or()
//                    .like("fee_status",ownerName.equals("未缴费")?0:1)
//                    .or()
//                    .like()
            List<Owner> list = mapper.selectList(queryWrapper);
            for (Owner owner : list) {
                Integer oid = owner.getId();
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("own_id", oid);
                Parking parking = parkingMapper.selectOne(queryWrapper1);
                if (parking==null){
                    continue;
                }
                parking.setOwnerName(owner.getOwnName());
                parking.setOwnerPhone(owner.getOwnPhone());
                parkings.add(parking);
            }

            QueryWrapper<Parking> queryWrapper1 = new QueryWrapper();
            queryWrapper1.like("fee_status","未缴费".contains(ownerName)?0:("已缴费".contains(ownerName)?1:""));
            List<Parking> parkings1 = parkingMapper.selectList(queryWrapper1);
            parkings.addAll(parkings1);

        }
        return ResponseResult.okResult(parkings);
    }

    @Override
    public ResponseResult delete(int id) {
        int i = parkingMapper.deleteById(id);
        if (i==1) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult add(Parking parking) {
        int i = parkingMapper.insert(parking);
        if (i==1) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult findAllParkNum() {

//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.select("parkNum");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("park_id");
        List<Owner> list1 = ownerMapper.selectList(queryWrapper);

        List<Park> list = new ArrayList<>();
        for (Owner s : list1){
            if (s==null){
                continue;
            }
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",s.getParkId());
            Park park = parkMapper.selectOne(queryWrapper1);
            list.add(park);
        }
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult selectOwnerByParkId(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("park_id",id);
        Owner owner = ownerMapper.selectOne(queryWrapper);
        return ResponseResult.okResult(owner);
    }


}
